package com.bjdd.jdy.ui.login;


import com.bjdd.jdy.Constant;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SignUtils;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StringUtils;
import com.bjdd.jdy.util.T;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import rx.functions.Action1;

/**
 * Created by yukai on 2016/11/12.
 */

public class LoginPresenter extends LoginContract.Presenter {

    @Override
    public void login(String account, String pwd) {
        String timestamp = SignUtils.getTime();
        /*try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(pwd.getBytes());
            pwd = new BigInteger(1,md.digest()).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }*/
        pwd = StringUtils.MD5(pwd);
        Map<String,String> map = new HashMap();
        map.put("mobile",account);
        map.put("pwd",pwd);
        map.put("timestamp",timestamp);
        String sign = SignUtils.getSignature(map);
        showLoadingDialog(false);
        rxManager.add(model.getLoginModel(account,pwd,timestamp,sign)
                .subscribe(result -> {

                    if(!result.isError()){
//                        SpUtil.setUser(result.getData());
//                        view.loginSuccess(result.getData());
//                        rxManager.post(Constant.EVENT_LOGIN,result.getData());
                        SpUtil.setToken(result.getData().getToken());
                        getUserInfo(result.getData().getToken(),result.getData().getUserId());
                    }else {
                        T.show(result.getMsg());
                        hideLoadingDialog();
                    }
                },e-> {
                    hideLoadingDialog();
                    T.show("登录失败");
                    e.printStackTrace();
                })
        );
    }

    @Override
    public void getUserInfo(String token, String userId) {
        rxManager.add(model.getUserInfo(token,userId)
                    .subscribe(result -> {
                        hideLoadingDialog();
                        if (!result.isError()){
                            SpUtil.setUser(result.getData());
                            view.loginSuccess(result.getData());
                        }else {
                            T.show(result.getMsg());
                        }
                    },e -> {
                        hideLoadingDialog();
                    }));
    }

    @Override
    public void getCode(String mobile) {
        String timestamp = SignUtils.getTime();
        Map<String,String> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("timestamp",timestamp);
        String sign = SignUtils.getSignature(map);
        showLoadingDialog(false);
        rxManager.add(model.getCodeModel(mobile,timestamp,sign)
                .subscribe(result -> {
                    hideLoadingDialog();
                    T.show(result.getMsg());
                },e -> {
                    hideLoadingDialog();
                    T.show("获取验证码失败");
                    L.d("获取验证码失败");
                }));
    }

    @Override
    public void codeLogin(String mobile, String code) {
        String timestamp = SignUtils.getTime();
        Map<String,String> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("code",code);
        map.put("timestamp",timestamp);
        String sign = SignUtils.getSignature(map);
        showLoadingDialog(false);
        rxManager.add(model.codeLogin(mobile,timestamp,code,sign)
                .subscribe(result -> {
                    if (!result.isError()){
                        SpUtil.setToken(result.getData().getToken());
                        getUserInfo(result.getData().getToken(),result.getData().getUserId());
                    }else {
                        hideLoadingDialog();
                        T.show(result.getMsg());
                    }

                },e -> {
                    hideLoadingDialog();
                    T.show("登陆失败");
                }));
    }

    @Override
    public void onStart() {
        rxManager.on(Constant.EVENT_LOGIN, new Action1<Object>() {
            @Override
            public void call(Object o) {
                L.d("Login","login success");
            }
        });
    }
}
